package ms.unicorn.diagram.edit.policies;

import ms.unicorn.diagram.edit.commands.EntityCreateCommand;
import ms.unicorn.diagram.providers.UnicornElementTypes;
import ms.unicorn.modellanguage.HintedSpecializationType;
import ms.unicorn.modellanguage.LanguageElement;
import ms.unicorn.modellanguage.LanguageEntity;

import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.ICommand;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.emf.commands.core.commands.DuplicateEObjectsCommand;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.emf.type.core.requests.CreateElementRequest;
import org.eclipse.gmf.runtime.emf.type.core.requests.DuplicateElementsRequest;

/**
 * @generated
 */
public class DiagramItemSemanticEditPolicy extends
		UnicornBaseItemSemanticEditPolicy {

	/**
	 * @generated
	 */
	public DiagramItemSemanticEditPolicy() {
		super(UnicornElementTypes.Diagram_1000);
	}

	/**
	 * @generated NOT
	 * Accept create element request of any Entity-base type
	 */
	protected Command getCreateCommand(CreateElementRequest req) {
		IElementType type = req.getElementType();
		if (type instanceof HintedSpecializationType) {
			HintedSpecializationType hintType = (HintedSpecializationType) type;
			LanguageElement e = hintType.getElement();
			if (e instanceof LanguageEntity) {
				ICommand creationCommand =  ((LanguageEntity)e).getEditHelperAdvice().getCreationCommand(hintType.getElement(), req);
				if (creationCommand != null)
					return getGEFWrapper(creationCommand);
			}
		}
		else if (UnicornElementTypes.Entity_2001 == req.getElementType()) {
			return getGEFWrapper(new EntityCreateCommand(req));
		}
		return super.getCreateCommand(req);
	}

	/**
	 * @generated
	 */
	protected Command getDuplicateCommand(DuplicateElementsRequest req) {
		TransactionalEditingDomain editingDomain = ((IGraphicalEditPart) getHost())
				.getEditingDomain();
		return getGEFWrapper(new DuplicateAnythingCommand(editingDomain, req));
	}

	/**
	 * @generated
	 */
	private static class DuplicateAnythingCommand extends
			DuplicateEObjectsCommand {

		/**
		 * @generated
		 */
		public DuplicateAnythingCommand(
				TransactionalEditingDomain editingDomain,
				DuplicateElementsRequest req) {
			super(editingDomain, req.getLabel(), req
					.getElementsToBeDuplicated(), req
					.getAllDuplicatedElementsMap());
		}

	}

}
